/**
  ******************************************************************************
  * @file   :   main.c
  * @version:
  * @author :
  * @brief  :
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) . BLUEX Microelectronics.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */

/* includes ------------------------------------------------------------------*/

#include "bx_kernel.h"
#include "user_app.h"
#include <easyflash.h>
#include "bx_shell.h"
#include "bx_service_spi.h"
#include "apollo_00_reg.h"
#include "bx_service_iic.h"
#include "bx_spi_type_def.h"
#include "bx_service_gpio.h"
#include "bx_service_tim.h"
#include "lis3dsh_iic.h"

//#include "oled.h"
/* private define ------------------------------------------------------------*/

uint32_t m_len_sent;
uint32_t m_cnt_10ms;
uint8_t m_data_array[250];

/* private typedef -----------------------------------------------------------*/

struct bx_spim_txrx_config * spim_data;
/* private variables ---------------------------------------------------------*/

extern s32 user_service_id;

/* exported variables --------------------------------------------------------*/

uint8_t iic_data[3] = {0x77, 0x02, 0x04};
uint8_t iic_readbuf[3] = {0x01, 0x02, 0x03};
uint8_t lis3_data[2] = {0x8f, 0x00};

uint8_t lis3_rx_data[2] = {0x01, 0x02};
uint8_t lis3_rx_data2[2] = {0x01, 0x02};

/*============================= private function =============================*/

/*========================= end of private function ==========================*/

/*============================= exported function ============================*/
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void user_init( void )
{
    bxs_iic_register();
}


/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
void user_app( void )
{

    /*-----------iic----------------*/
    bx_call( bxs_iic0_id(), BXM_OPEN, 0, 0 );
    bx_set( bxs_iic0_id(), BXP_IIC_PIN_SCL, 2, 0 );
    bx_set( bxs_iic0_id(), BXP_IIC_PIN_SDA, 3, 0 );
    bx_set( bxs_iic0_id(), BXP_ADDR, 0x3d, 0 );
    bx_set( bxs_iic0_id(), BXP_SUB_ADDR, 0x20, 0 );
    bx_set( bxs_iic0_id(), BXP_SPEED, BX_IIC_SPEED_STANDARD, 0 );
    bx_set( bxs_iic0_id(), BXP_ADDR_BIT, BX_IIC_ADDR_BIT_7B, 0 );
    bx_set( bxs_iic0_id(), BXP_MODE, BX_IIC_MODE_MASTER, 0 );
    iic_read_lis3dsh( 0x0f, iic_readbuf, 3 );
    if( iic_readbuf[0] == UC_WHO_AM_I_DEFAULT_VALUE ) {
        bxsh_log( "device ID=%02x\r\n", iic_readbuf[0] );
        iic_write_lis3dsh( ADD_REG_CTRL_4, iic_data, 3 );
        iic_read_lis3dsh( ADD_REG_CTRL_4, iic_readbuf, 3 );
        if( iic_readbuf[0] == 0x77 ) {
            bxsh_log( "set success\r\n" );
            bx_repeat( user_service_id, BXM_IIC_TEST, 0, 0, 1000 );

        }

    } else {
        bxsh_log( "device get failed\r\n" );
    }

}
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
bx_err_t user_msg_handle_func( s32 svc, u32 msg, u32 param0, u32 param1 )
{

    switch( msg ) {
        case BXM_IIC_TEST:
            iic_read_lis3dsh( 0x28, iic_test_buf, 1 );
            bxsh_log( "XL=%d", iic_test_buf[0] );
            iic_read_lis3dsh( 0x29, iic_test_buf, 1 );
            bxsh_log( " XH=%d\r\n", iic_test_buf[0] );
            iic_read_lis3dsh( 0x2a, iic_test_buf, 1 );
            bxsh_log( "YL=%d", iic_test_buf[0] );
            iic_read_lis3dsh( 0x2b, iic_test_buf, 1 );
            bxsh_log( " YH=%d\r\n", iic_test_buf[0] );
            iic_read_lis3dsh( 0x2c, iic_test_buf, 1 );
            bxsh_log( "ZL=%d", iic_test_buf[0] );
            iic_read_lis3dsh( 0x2d, iic_test_buf, 1 );
            bxsh_log( " ZH=%d\r\n", iic_test_buf[0] );
            break;
        default:
            break;
    }



    if( bx_msg_source() == bxs_iic0_id() ) {
        switch( msg ) {
            case BXM_READ_DONE:

                break;
            case BXM_WRITE_DONE:

                break;


            default:
                break;
        }
    }

    return BX_OK;
}
/*========================= end of exported function =========================*/


/*============================= import function ==============================*/

/*=========================== end of import function =========================*/


/*============================ interrupt function ============================*/

/*========================= end of interrupt function ========================*/


/******************** (C) COPYRIGHT BLUEX **********************END OF FILE****/



